using Microsoft.EntityFrameworkCore;
using RestfulApi.Models;

namespace RestfulApi.Data
{
    /// <summary>
    /// 图书仓储接口
    /// </summary>
    public interface IBookRepository
    {
        /// <summary>
        /// 获取所有图书
        /// </summary>
        /// <returns>图书列表</returns>
        Task<IEnumerable<Book>> GetAllBooksAsync();

        /// <summary>
        /// 根据ID获取图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>图书对象，如果不存在则返回null</returns>
        Task<Book?> GetBookByIdAsync(int id);

        /// <summary>
        /// 添加图书
        /// </summary>
        /// <param name="book">图书对象</param>
        /// <returns>添加后的图书对象</returns>
        Task<Book> AddBookAsync(Book book);

        /// <summary>
        /// 更新图书
        /// </summary>
        /// <param name="book">图书对象</param>
        /// <returns>是否更新成功</returns>
        Task<bool> UpdateBookAsync(Book book);

        /// <summary>
        /// 删除图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>是否删除成功</returns>
        Task<bool> DeleteBookAsync(int id);

        /// <summary>
        /// 根据作者查询图书
        /// </summary>
        /// <param name="author">作者名</param>
        /// <returns>图书列表</returns>
        Task<IEnumerable<Book>> GetBooksByAuthorAsync(string author);

        /// <summary>
        /// 根据分类查询图书
        /// </summary>
        /// <param name="category">分类名</param>
        /// <returns>图书列表</returns>
        Task<IEnumerable<Book>> GetBooksByCategoryAsync(string category);

        /// <summary>
        /// 保存所有更改
        /// </summary>
        /// <returns>是否保存成功</returns>
        Task<bool> SaveChangesAsync();
    }

    /// <summary>
    /// 图书仓储实现类
    /// </summary>
    public class BookRepository : IBookRepository
    {
        private readonly ApplicationDbContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public BookRepository(ApplicationDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有图书
        /// </summary>
        /// <returns>图书列表</returns>
        public async Task<IEnumerable<Book>> GetAllBooksAsync()
        {
            return await _context.Books.ToListAsync();
        }

        /// <summary>
        /// 根据ID获取图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>图书对象，如果不存在则返回null</returns>
        public async Task<Book?> GetBookByIdAsync(int id)
        {
            return await _context.Books.FindAsync(id);
        }

        /// <summary>
        /// 添加图书
        /// </summary>
        /// <param name="book">图书对象</param>
        /// <returns>添加后的图书对象</returns>
        public async Task<Book> AddBookAsync(Book book)
        {
            var result = await _context.Books.AddAsync(book);
            return result.Entity;
        }

        /// <summary>
        /// 更新图书
        /// </summary>
        /// <param name="book">图书对象</param>
        /// <returns>是否更新成功</returns>
        public async Task<bool> UpdateBookAsync(Book book)
        {
            var existingBook = await _context.Books.FindAsync(book.Id);
            if (existingBook == null)
                return false;

            _context.Entry(existingBook).CurrentValues.SetValues(book);
            return true;
        }

        /// <summary>
        /// 删除图书
        /// </summary>
        /// <param name="id">图书ID</param>
        /// <returns>是否删除成功</returns>
        public async Task<bool> DeleteBookAsync(int id)
        {
            var book = await _context.Books.FindAsync(id);
            if (book == null)
                return false;

            _context.Books.Remove(book);
            return true;
        }

        /// <summary>
        /// 根据作者查询图书
        /// </summary>
        /// <param name="author">作者名</param>
        /// <returns>图书列表</returns>
        public async Task<IEnumerable<Book>> GetBooksByAuthorAsync(string author)
        {
            return await _context.Books
                .Where(b => b.Author.Contains(author))
                .ToListAsync();
        }

        /// <summary>
        /// 根据分类查询图书
        /// </summary>
        /// <param name="category">分类名</param>
        /// <returns>图书列表</returns>
        public async Task<IEnumerable<Book>> GetBooksByCategoryAsync(string category)
        {
            return await _context.Books
                .Where(b => b.Category != null && b.Category.Contains(category))
                .ToListAsync();
        }

        /// <summary>
        /// 保存所有更改
        /// </summary>
        /// <returns>是否保存成功</returns>
        public async Task<bool> SaveChangesAsync()
        {
            return await _context.SaveChangesAsync() > 0;
        }
    }
}